<?php
namespace app\erp\controller;

use app\erp\model\Partnerdb;

class Partner extends Base
{

    protected $cols = array(
        array(
            'field' => 'partner_id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'partner_name',
            'width' => 300,
            'title' => '合作伙伴',
            'sort' => 'true'
        ),
        array(
            'field' => 'start_time',
            'width' => 120,
            'title' => '合作开始时间',
            'sort' => 'true'
        ),
        array(
            'field' => 'end_time',
            'width' => 120,
            'title' => '合作结束时间',
            'sort' => 'true'
        ),
        array(
            'field' => 'amount',
            'width' => 120,
            'title' => '合同金额',
            'sort' => 'true'
        ),
        array(
            'field' => 'web_url',
            'width' => 200,
            'title' => '主站域名',
            'sort' => 'true'
        ),
        array(
            'fixed' => 'right',
            'width' => 80,
            'toolbar' => '#tool'
        )
    );

    public function index()
    {
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    /**
     *
     * @todo 列表
     */
    public function ajaxPartner()
    {
        $request = request();
        $param = $request->param();
        $partner = new Partnerdb();
        $return = $partner->ajaxSelect($param);
        $this->layuiReturn($return['data'], $return['count']);
    }

    /**
     *
     * @todo 软删除
     */
    public function ajaxDelete()
    {
        $request = request();
        $param = $request->param();
        $partner = new Partnerdb();
        $partner->ajaxDelete($param);
        $this->layuiReturn('删除成功', 0);
    }

    /**
     *
     * @todo 添加修改表单
     */
    public function tpl_form()
    {
        $request = request();
        $param = $request->param();
        $data = array();
        $partner_id = '';
        if (isset($param['partner_id']) && ! empty($param['partner_id'])) {
            // 获取数据
            $partner = new Partnerdb();
            $data = $partner->ajaxFind($param['partner_id']);
            $partner_id = $param['partner_id'];
        }
        $this->assign('data', json_encode($data));
        $this->assign('partner_id', $partner_id);
        return $this->fetch();
    }

    public function ajaxOneData()
    {
        $request = request();
        $param = $request->param();
        $partner = new Partnerdb();
        $data = $partner->ajaxFind($param['partner_id']);
        $this->layuiReturn($data, 1);
    }

    /**
     *
     * @todo 保存数据
     */
    public function saveData()
    {
        $request = request();
        $param = $request->param();
        $partner = new Partnerdb();
        $return = $partner->ajaxSave($param);
        if ($return != 'success') {
            $this->layuiReturn($return, 0, $return, - 1);
        } else {
            $this->layuiReturn('保存成功');
        }
    }
}